﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class BranchController : Controller
    {
        //
        // GET: /Branch/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// select branch by branch id (used to get branches coresponding to the branch)
        /// </summary>
        /// <returns>json array </returns>
        public ActionResult GetBranch()
        {
            decimal idCompany = 0;
            Decimal.TryParse(Request.Params["companyId"], out idCompany);

            BranchContainer container = BranchModels.GetBranchArray(idCompany);

            return Json(container, JsonRequestBehavior.AllowGet);
        }

		/// <summary>
		/// modify branch infos
		/// </summary>
		/// <returns></returns>
        public ActionResult EditBranch()
        {
            BranchModels branch = new BranchModels();

            decimal idBranch = 0;
            Decimal.TryParse(Request.Params["Id"], out idBranch);

            decimal idCity = 0;
            Decimal.TryParse(Request.Params["IdCity"], out idCity);

            decimal idCompany = 0;
            Decimal.TryParse(Request.Params["IdComp"], out idCompany);

            branch.IdBranch = idBranch;
            branch.IdCity = idCity;
            branch.IdCompany = idCompany;
            branch.BranchName = Request.Params["BranchName"];
            branch.Country = Request.Params["Country"];
            branch.Address =Request.Params["Address"];
            branch.AddressDetail = Request.Params["AddressDetail"];

            BranchModels checkBranch = new BranchModels();
            checkBranch = BranchModels.CheckBranch(branch);

            BranchModels checkBranchEdit = new BranchModels();
            checkBranchEdit = BranchModels.CheckBranchEdit(branch);

            string messageBack = null;

            try
            {
                if (idBranch == 0)
                {
                    if (checkBranch == null)
                    {
                        BranchModels.AddBranch(branch);
                    }
                    else
                    {
                        messageBack = "Sucursala exista deja";
                    }
                }
                else
                {
                    if (idBranch != 0)
                    {
                        if (checkBranchEdit == null)
                        {
                            BranchModels.EditBranch(branch);
                        }
                        else
                        {
							messageBack = "Sucursala exista deja";
                        }
                    }
                }
                return Json(new { success = true, description = messageBack });
            }
            catch (Exception ex)
            {
				if (ex.Message.Contains("unique key constraint"))
				{
					messageBack = "Exista deja o inregistrare cu acelasi nume";
				}
				return Json(new { success = true, description = messageBack });
            }
        }

        /// <summary>
        /// select branch by id (used to get branch info for populate edit form when branch is selected)
        /// </summary>
        /// <returns>json array with one record</returns>
        public ActionResult GetBranchById()
        {
            decimal idBranch = 0;
            Decimal.TryParse(Request.Params["Id"], out idBranch);

            BranchContainer container = BranchModels.GetBranchArrayById(idBranch);

            return Json(container, JsonRequestBehavior.AllowGet);
        }
    }
}
